package main

import (
	"encoding/json"
	"fmt"
)

/*
   因为结构体中私有属性不能被json包使用，进而导致转换后成功的都是大写开头
   标签 解决该类问题
*/

type Student struct {
	ID     string `json:"id"`
	Gender string `json:"gender"`
	Name   string `json:"name"`
}

func main() {
	/*
	   结构体转换 json
	*/
	var stu1 = Student{
		ID:     "001",
		Gender: "男",
		Name:   "小王子",
	}
	jsonByte, err := json.Marshal(stu1)
	if err != nil {
		fmt.Println("json err = ", err)
	}
	jsonStr := string(jsonByte)
	fmt.Printf("%v\n", jsonStr)
	//{"id":"001","gender":"男","name":"小王子"}

	/*
	  json 转换成 结构体
	*/
	var str = `{
		"id":"002",
		"Gender":"女",
		"name":"娜扎"
	}`
	var stu2 Student
	err2 := json.Unmarshal([]byte(str), &stu2)

	if err2 != nil {
		fmt.Println("json err = ", err2)
	}
	fmt.Printf("%#v\n", stu2)
	//main.Student{ID:"002", Gender:"女", Name:"娜扎"}

}
